<?php

namespace app\robot\controller;

use app\model\Base;
use support\Redis;
use support\Request;

class learntController
{

    //robot/learnt/checknextlogin
    public function checknextlogin(Request $request)
    {
        $post = $request->get();

        $host = getenv("host");
        $url = "{$host}robot/learnt/checknextlogin";
        $rt = Base::curlpostdata($url, $post);

        $rdata = json_encode($rt);
        $response = response();
        return $response->withBody($rdata);
    }

    public function allocateframe(Request $request)
    {
        $post = $request->get();

        $host = getenv("host");
        $url = "{$host}robot/learnt/allocateframe";
        $rt = Base::curlpostdata($url, $post);

        $rdata = json_encode($rt);
        $response = response();
        return $response->withBody($rdata);
    }

    public function sendqywxgroup(Request $request)
    {
        $post = $request->get();

        $host = getenv("host");
        $url = "{$host}robot/learnt/sendqywxgroup";
        $rt = Base::curlpostdata($url, $post);

        $rdata = json_encode($rt);
        $response = response();
        return $response->withBody($rdata);
    }
    public static function getDeviceInfoByFd($fd)
    {
        $fdkey = "deviceinfobyfd:{$fd}";

        $deviceinfo = Base::redisGetArr($fdkey);

        return $deviceinfo;
    }
    //robot/learnt/checkframe

    public function checkframe(Request $request)
    {
        $post = $request->get();
        $frameid = $post["frameid"];
        $key = "getrobotfdbyframeid:{$frameid}";
        $fd = Base::redisGet($key);
        if ($fd) {
            $robotinfo = self::getDeviceInfoByFd($fd);
            if (!$robotinfo) {
                $rt["code"] = 2;
                $rt["msg"] = "库内车辆掉线";
                $rdata = json_encode($rt);
                $response = response();
                return $response->withBody($rdata);
            }
            $robotimei = $robotinfo["imei"];
            $car_number = $robotinfo["car_number"];
            $rt["code"] = 1;
            $rt["data"]["inframeimei"] = $robotimei;
            $rt["data"]["robotinfo"] = $robotinfo;
            $rt["msg"] = "{$car_number}在此框({$frameid})内";
            $rdata = json_encode($rt);
            $response = response();
            return $response->withBody($rdata);
        } else {
            $rt["code"] = 2;
            $rt["data"] = [];
            $rt["msg"] = "没有机器人在此框内";
            $rdata = json_encode($rt);
            $response = response();
            return $response->withBody($rdata);
        }
    }


    public function finishlevel(Request $request)
    {
        $post = $request->get();

        $host = getenv("host");
        $url = "{$host}robot/learnt/finishlevel";
        $rt = Base::curlpostdata($url, $post);

        $rdata = json_encode($rt);

        $response = response();
        return $response->withBody($rdata);
    }


    public function getsimpass(Request $request)
    {

        //robot/learnt/getsimpass
        $post = $request->get();

        $host = getenv("host");
        $url = "{$host}robot/learnt/getsimpass";
        $rt = Base::curlpostdata($url, $post);

        $rdata = json_encode($rt);

        $response = response();
        return $response->withBody($rdata);
    }


    public function heartbeat(Request $request)
    {


        $post = $request->get();

        $host = getenv("host");
        $url = "{$host}robot/learnt/heartbeat";
        $rt = Base::curlpostdata($url, $post);

        $rdata = json_encode($rt);
        $response = response();
        return $response->withBody($rdata);
    }

    public function getlevel(Request $request)
    {
        $post = $request->get();

        $host = getenv("host");
        $url = "{$host}robot/learnt/getlevel";
        $rt = Base::curlpostdata($url, $post);

        $rdata = json_encode($rt);

        $response = response();
        return $response->withBody($rdata);
    }

    public function clutch(Request $request)
    {
        $post = $request->get();

        $host = getenv("host");
        $url = "{$host}robot/learnt/clutch";
        $rt = Base::curlpostdata($url, $post);

        $rdata = json_encode($rt);
        $response = response();
        return $response->withBody($rdata);
    }

    public function startpass(Request $request)
    {
        $post = $request->get();

        $host = getenv("host");
        $url = "{$host}robot/learnt/startpass";
        $rt = Base::curlpostdata($url, $post);

        $rdata = json_encode($rt);

        $response = response();
        return $response->withBody($rdata);
    }

    public function endpass(Request $request)
    {
        $post = $request->get();
        $host = getenv("host");
        $url = "{$host}robot/learnt/endpass";
        $rt = Base::curlpostdata($url, $post);
        $rdata = json_encode($rt);
        $response = response();
        return $response->withBody($rdata);
    }

    public function robotwarning(Request $request)
    {

        $post = $request->post();
        $host = getenv("host");
        $url = "{$host}robot/learnt/robotwarning";
        $rt = Base::curlpostdata($url, $post);

        $rdata = json_encode($rt);
        $response = response();
        return $response->withBody($rdata);
    }

    public function adderror(Request $request)
    {
        $post = $request->get();


        $host = getenv("host");
        $url = "{$host}robot/learnt/adderror";
        $rt = Base::curlpostdata($url, $post);

        $rdata = json_encode($rt);
        $response = response();
        return $response->withBody($rdata);
    }

    public function uploadtrack(Request $request)
    {


        $all = $request->all();

        $host = getenv("host");
        $url = "{$host}robot/learnt/uploadtrack";

        $rt = Base::curlpostdata($url, json_encode($all));

        $rdata = json_encode($rt);
        $response = response();
        return $response->withBody($rdata);
    }

    public function startexam(Request $request)
    {
        $post = $request->get();
        $host = getenv("host");
        $url = "{$host}robot/learnt/startexam";
        $rt = Base::curlpostdata($url, $post);

        $rdata = json_encode($rt);
        $response = response();
        return $response->withBody($rdata);
    }

    public function endexam(Request $request)
    {
        $post = $request->get();
        $host = getenv("host");
        $url = "{$host}robot/learnt/endexam";
        $rt = Base::curlpostdata($url, $post);

        $rdata = json_encode($rt);
        $response = response();
        return $response->withBody($rdata);
    }

    public function startstage(Request $request)
    {

        $post = $request->get();

        $host = getenv("host");
        $url = "{$host}robot/learnt/startstage";
        $rt = Base::curlpostdata($url, $post);

        $rdata = json_encode($rt);
        $response = response();
        return $response->withBody($rdata);
    }


    public function endstage(Request $request)
    {
        $post = $request->all();

        $host = getenv("host");
        $url = "{$host}robot/learnt/endstage";
        $rt = Base::curlpostdata($url, $post);

        $rdata = json_encode($rt);
        $response = response();
        return $response->withBody($rdata);
    }

    public function addpoint(Request $request)
    {

        $post = $request->get();

        $host = getenv("host");
        $url = "{$host}robot/learnt/addpoint";

        $rt = Base::curlpostdata($url, $post);

        $rdata = json_encode($rt);
        $response = response();
        return $response->withBody($rdata);
    }

    public function uploadscreen(Request $request)

    {
        $post = $request->all();

        $host = getenv("host");
        $url = "{$host}robot/learnt/uploadscreen";
        $rt = Base::curlpostdata($url, $post);

        $rdata = json_encode($rt);
        $response = response();
        return $response->withBody($rdata);
    }

    public function uploadface(Request $request)
    {

        $post = $request->all();

        $host = getenv("host");
        $url = "{$host}robot/learnt/uploadface";
        $rt = Base::curlpostdata($url, $post);

        $rdata = json_encode($rt);
        $response = response();
        return $response->withBody($rdata);
    }

    public function gettrack(Request $request)
    {

        $post = $request->all();
        $host = getenv("host");
        $url = "{$host}robot/learnt/gettrack";
        $rt = Base::curlpostdata($url, $post);
        $rdata = json_encode($rt);
        $response = response();
        return $response->withBody($rdata);
    }

    //http://192.168.0.200:18306/robot/learnt/login
    //http://192.168.100.175:18306/robot/learnt/login
    public function login(Request $request)
    {

        $post = $request->get();


        $host = getenv("host");
        $url = "{$host}robot/learnt/login";
        $rt = Base::curlpostdata($url, $post);
        $rdata = json_encode($rt);
        $response = response();
        return $response->withBody($rdata);
    }

    //http://192.168.100.175:8787/robot/learnt/logout
    public function logout(Request $request)
    {

        $post = $request->post();


        $get = $request->get();
       
        $key=$get["key"];

        $host = getenv("host");
        $url = "{$host}robot/learnt/logout?key={$key}";
    
        $rt = Base::curlpostdata($url, json_encode($post));
        $rdata = json_encode($rt);
        $response = response();
        return $response->withBody($rdata);
    }


    public function robtlist(Request $request)
    {
        //192.168.100.112:18306/sur/surlist
        $keys = "deviceinfobyfd:*";

        $list = Redis::keys($keys);

        $surlist = [];
        $response = response();

        if ($list) {

            foreach ($list as $key) {
                $key = trim($key, "swoft:");
                $deviceinfo = Redis::get($key);

                if (!isset($deviceinfo["role"])) {

                    $json = json_encode($deviceinfo, JSON_UNESCAPED_UNICODE);
                    return $response->withBody($json);
                }
                $role = $deviceinfo["role"];


                if ($role == "robot_coach") {
                    $surlist[] = $deviceinfo;
                }
            }
        }
        $json = json_encode($surlist, JSON_UNESCAPED_UNICODE);

        return $response->withBody($json);
    }


    public function uploadRobotOssData(Request $request)
    {



        $data = $request->all();


        $rediskey = "RobotData";

        Redis::lPush($rediskey, json_encode($data));

        $school_id = $data["schoolId"];
        $studentId = $data['studentId'];
        $robotId = $data["robotId"];

        $rsp = [
            'code' => 0,
            'msg' => '数据上传成功',
            'trace' => '',
            "data" => [
                "school_id" => $school_id,
                "student_id" => $studentId,
                "robot_id" => $robotId,
            ]
        ];


        $rdata = json_encode($rsp);
        $response = response();
        return $response->withBody($rdata);
    }
}
